package com.dianganjiao.lib.http

import com.dianganjiao.lib.AppContext
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.impl.LoadingPopupView
import com.lzy.okgo.request.base.Request

abstract class LoadingHttpCallback<T> : HttpCallback<T>() {

    private var loading: LoadingPopupView? = null

    override fun onStart(request: Request<Any, out Request<Any, Request<*, *>>>?) {
        loading?.dismiss()
        val currentActivity = AppContext.appContext.getCurrentActivity()
        if(currentActivity != null) {
            loading = XPopup.Builder(currentActivity)
                .hasShadowBg(false)
                .asLoading(getLoadingTitle())
                .show() as LoadingPopupView
        }
    }

    open fun getLoadingTitle():String{
        return "正在加载中..."
    }

    override fun onFinish() {
        loading?.dismiss()
        tag?.let {
            OkGoApi.api.cancelTag(tag.toString())
        }
    }
}